package view;

import entity.Supplier;
import org.jb2011.lnf.beautyeye.ch3_button.BEButtonUI;
import services.Service;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.text.SimpleDateFormat;
import java.util.Date;

public class AddSupplierrJFrame extends JFrame implements MouseListener {
    // 定义全局组件
    JPanel backgroundPanel, labelPanel, contentPanel, buttonPanel;
    JLabel labelName, labelTel, labelMail, labelAddr, labelJoinDate;
    JTextField name, tel, mail, addr, joinDate;
    JButton buttonAdd;

    Service service;
    // 父面板对象
    SupplierManagerJPanel parentPanel;
    public AddSupplierrJFrame(Service service, SupplierManagerJPanel supplierManagerJPanel) {
        this.service = service;
        this.parentPanel = supplierManagerJPanel;

        initBackgroundPanel();

        this.add(backgroundPanel);

        this.setTitle("添加供应商信息");
        this.setSize(640, 360);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
    }
    // 初始化背景面板
    public void initBackgroundPanel() {
        backgroundPanel = new JPanel(new BorderLayout());

        initLabelPanel();
        initContentPanel();
        initButtonPanel();

        backgroundPanel.add(labelPanel, "North");
        backgroundPanel.add(contentPanel, "Center");
        backgroundPanel.add(buttonPanel, "South");
    }
    // 初始化label面板
    public void initLabelPanel() {

        labelPanel = new JPanel();

        JLabel title = new JLabel("供应商信息");
        title.setFont(new Font("微软雅黑", Font.PLAIN, 14));

        labelPanel.add(title);
    }

    // 初始化商品信息面板
    public void initContentPanel() {
        contentPanel = new JPanel(new GridLayout(6, 1));

        labelName = new JLabel("姓名", JLabel.CENTER);
        labelTel = new JLabel("电话", JLabel.CENTER);
        labelMail = new JLabel("邮箱", JLabel.CENTER);
        labelAddr = new JLabel("地址", JLabel.CENTER);
        labelJoinDate = new JLabel("录入时间", JLabel.CENTER);

        name = new JTextField("");
        tel = new JTextField("");
        mail = new JTextField("");
        addr = new JTextField("");
        joinDate = new JTextField("");


        contentPanel.add(labelName);
        contentPanel.add(name);
        contentPanel.add(labelTel);
        contentPanel.add(tel);
        contentPanel.add(labelMail);
        contentPanel.add(mail);
        contentPanel.add(labelAddr);
        contentPanel.add(addr);
        contentPanel.add(labelJoinDate);
        contentPanel.add(joinDate);
    }

    // 初始化按钮面板
    public void initButtonPanel() {
        buttonPanel = new JPanel();

        buttonAdd = new JButton("保存");
        buttonAdd.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.lightBlue));
        buttonAdd.setForeground(Color.white);
        buttonAdd.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        buttonAdd.addMouseListener(this);

        buttonPanel.add(buttonAdd);
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        if (e.getSource() == buttonAdd) {
            String nameString = name.getText().trim();
            String telString = tel.getText().trim();
            String mailString = mail.getText().trim();
            String addrString = addr.getText().trim();
            String joinDateString = joinDate.getText().trim();

            if (nameString.isEmpty()) {
                JOptionPane.showMessageDialog(null, "请输入姓名");
            } else if (telString.isEmpty()) {
                JOptionPane.showMessageDialog(null, "请输入电话");
            } else if (mailString.isEmpty()) {
                JOptionPane.showMessageDialog(null, "请输入邮箱");
            } else if (addrString.isEmpty()) {
                JOptionPane.showMessageDialog(null, "请输入地址");
            }  else if (joinDateString.isEmpty()) {
                JOptionPane.showMessageDialog(null, "请输入录入时间");
            } else {
                int result = 0;
                String idString = ""+System.currentTimeMillis(); // 在java中是最常用的获取系统时间的方法，它返回的是1970年1月1日0点到现在经过的毫秒数;
                try {
                    Date joinDateDate = (new SimpleDateFormat("yy-MM-dd")).parse(joinDateString);
                    Supplier supplier = new Supplier(idString,nameString,telString,mailString,addrString,
                            joinDateDate,null,0);
                    result = service.insertById(1, supplier);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }

                if (result > 0) {
                    JOptionPane.showMessageDialog(null, "添加成功");
                    this.setVisible(false);
                    parentPanel.refreshTablePanel();
                }
            }
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }

}
